/**
 * Created by Administrator on 14-3-27.
 */
 var state=function(){
    this.x;
    this.y;
}
var unit=function(){
    var player;
    var enemy;
    var npc,scratch;
    var d;
    this.bind =function(who,options){

        if(who==='player'){
        player=options;
             player.bind(playerrun);
        }else if(who==='enemy'){
        enemy=options;
            enemy.bind(enemyrun);
        }else{

        npc=options;
            if(options)
            npc.bind(npcrun);
        }
    }
    this.getPosition=function(who){

        var tem=new state;
        if(who==='player'){
            d.clone( player.state.pos );
            tem.x= d.get(0);tem.y= d.get(1);

        }else if(who==='enemy'){
            d.clone( enemy.state.pos );
            tem.x= d.get(0);tem.y= d.get(1);

        }else{
            d.clone( npc.state.pos );
            tem.x= d.get(0);tem.y= d.get(1);
        }
        return tem;
    }
    this.getVel=function(who){

        var tem=new state;
        if(who==='player'){
            d.clone( player.state.vel );
            tem.x= d.get(0);tem.y= d.get(1);

        }else if(who==='enemy'){
            d.clone( enemy.state.vel );
            tem.x= d.get(0);tem.y= d.get(1);

        }else{
            d.clone( npc.state.vel );
            tem.x= d.get(0);tem.y= d.get(1);
        }
        return tem;
    }
    this.getAngular=function(who){
        if(who==='player'){
            return player.state.angular.pos ;

        }else if(who==='enemy'){
            return ( enemy.state.angular.pos );

        }else{
            return ( npc.state.angular.pos );

        }
        return tem;
    }
    this.begin=function (){
         scratch = Physics.scratchpad();
        d = scratch.vector();
    }
    this.end=function(){
        scratch.done();
    }

};

Physics.body('player', 'circle', function( parent ){

    var deg = Math.PI/180;
     this.player = new Image();
    this. enemy = new Image();
    player.src = 'images/player.png';
    enemy.src ='images/enemy.png';
    this.run=null;
    console.log(player,enemy);
    var Pi2 = 2 * Math.PI;
    // will give a random polygon that, for small jitter, will likely be convex
    this.run;this.start;this.turn_n;this.goahead_n;this.attack_n;
    var shoot=0;//子弹数目
    return {
        // we want to do some setup when the body is created
        // so we need to call the parent's init method
        // on "this"
        bind :function(options){
            if(!this.run)
    this.run=options;
            if(this.gameType==='player')
                this.view = player;
            else if(enemy.src)
                this.view =enemy;

        },
        init: function( options ){
            parent.init.call( this, options );
            // set the rendering image
            // because of the image i've chosen, the nose of the ship
            // will point in the same angle as the body's rotational position


        },
        // this will turn the ship by changing the
        // body's angular velocity to + or - some amount
        turn: function( amount ){
            // set the ship's rotational velocity
            while (amount>=Math.PI)
            amount-=Math.PI;
            while (amount<=-Math.PI)
                amount+=Math.PI;

            this.state.angular.vel = 0.2 * amount * deg;

            var angle = this.state.angular.pos;
            var scratch = Physics.scratchpad();
            var  v; var d=scratch.vector();var t=d;
            d.clone(this.state.vel);
             v=Math.sqrt(d.get(0)* d.get(0)+ d.get(1)* d.get(1));

            t.set(v*Math.cos(angle),v*Math.sin(angle));
                /*  this.state.vel=t;*/

            scratch.done();
            return this;
        },
        // this will accelerate the ship along the direction
        // of the ship's nose
        goahead : function( amount ){
            var self = this;
            var world = this._world;
            if (!world){
                return self;
            }
            var angle = this.state.angular.pos;
            var scratch = Physics.scratchpad();
            // scale the amount to something not so crazy
            amount *= 0.00001;
            // point the acceleration in the direction of the ship's nose
            var v = scratch.vector().set(
                amount * Math.cos( angle ),
                amount * Math.sin( angle )
            );
            // accelerate self
            this.accelerate( v );
            scratch.done();

            // if we're accelerating set the image to the one with the thrusters on

            return this;
        },
        // this will create a projectile (little circle)
        // that travels away from the ship's front.
        // It will get removed after a timeout
        attack: function(){

            if(shoot>map.shoot)
                return this;
            shoot++;

            var self = this;
            var world = this._world;
            if (!world){
                return self;
            }
            var angle = this.state.angular.pos;
            var cos = Math.cos( angle );
            var sin = Math.sin( angle );
            var r = this.geometry.radius + 5;
            // create a little circle at the nose of the ship
            // that is traveling at a velocity of 0.5 in the nose direction
            // relative to the ship's current velocity
            var laser = Physics.body('circle', {
                x: this.state.pos.get(0) + r * cos,
                y: this.state.pos.get(1) + r * sin,
                vx: (0.5 + this.state.vel.get(0)) * cos,
                vy: (0.5 + this.state.vel.get(1)) * sin,
                radius: 5
            });
            // set a custom property for collision purposes
            laser.gameType = 'laser';

            // remove the laser pulse in 600ms
            setTimeout(function(){
                world.removeBody( laser );
                shoot--;
                laser = undefined;
            }, 1200);
            world.add( laser );
            return this;
        },

        die: function(){
            var self = this;
            var world = this._world;
            if (!world){
                return self;
            }

            // remove player
            world.removeBody( this );
            return self;
        }

    };
});
